package com.lanlian.cayman.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lanlian.cayman.R;
import com.lanlian.cayman.adapter.BankAdapter;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.VerifyMobileDialog;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BankInfo;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqBankList;
import com.lanlian.commonlib.bean.ReqDeleteBank;
import com.lanlian.commonlib.bean.ReqSmsCode;
import com.lanlian.commonlib.bean.RespBankList;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.uikit.ActionItem;
import com.lanlian.uikit.CustomBottomSheetDialog;
import com.lanlian.uikit.CustomDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/11/6 16:34
 *     desc   : 银行卡页面
 *     version: 1.0
 * </pre>
 */
public class BankActivity extends BaseActivity {

    private SmartRefreshLayout mLayRefresh;
    private RecyclerView mRecyclerView;

    private BankAdapter mAdapter;
    private VerifyMobileDialog mVerifyMobileDialog;
    private List<BankInfo> mList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_card_list, true);
        initView();
        setListeners();
        initAdapter();
        mLayRefresh.autoRefresh();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("银行卡");
        mLayRefresh = findViewById(R.id.lay_refresh);
        mRecyclerView = findViewById(R.id.recycler_view);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mLayRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mRecyclerView.scrollToPosition(0);
                getBankList();
            }
        });
    }

    /**
     * 初始化银行卡Adapter
     */
    private void initAdapter() {
        mAdapter = new BankAdapter(R.layout.adapter_bank_card, mList);
        mRecyclerView.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        View footView = LinearLayout.inflate(this, R.layout.view_bank_list_foot, null);
        footView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(BankActivity.this, AddBankActivity.class), 1234);
            }
        });
        mAdapter.addFooterView(footView);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mList.size() == 1) {
                    return;
                }
                CustomBottomSheetDialog dialog = new CustomBottomSheetDialog(BankActivity.this);
                dialog.addAction(new ActionItem("删除", R.color.color_text_style18));
                dialog.setOnItemClickListener(new CustomBottomSheetDialog.OnItemOnClickListener() {
                    @Override
                    public void onItemClick(ActionItem item, int pos) {
                        switch (item.getTitle()) {
                            case "删除":
                                VolleyRequestUtils.getInstance().sendSmsCode(this.toString(),
                                        new ReqSmsCode(mDBManager.getUser().getMobileNo()), new INetWorkCallback() {
                                            @Override
                                            public void onSuccess(Object response) {
                                                showToast("验证码已发送至您的手机");
                                                mVerifyMobileDialog = new VerifyMobileDialog(BankActivity.this, mDBManager.getUser().getMobile(),
                                                        new VerifyMobileDialog.OnCompleteListener() {
                                                            @Override
                                                            public void onComplete(VerifyMobileDialog verifyMobileDialog, String content) {
                                                                deleteBank(mList.get(position).getId(), content);
                                                            }
                                                        });
                                                mVerifyMobileDialog.show();
                                            }

                                            @Override
                                            public void onFail(BaseResponse baseResponse) {
                                                showToast(baseResponse.getMsg());
                                            }
                                        });
                                break;
                            default:
                                break;
                        }
                    }
                });
                dialog.showDialog();
            }
        });
    }

    /**
     * 获取银行卡列表
     */
    private void getBankList() {
        ReqBankList reqBankList = new ReqBankList();
        reqBankList.setType(1);
        VolleyRequestUtils.getInstance().getBankList(this.toString(), reqBankList, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                mLayRefresh.finishRefresh();
                RespBankList respBankList = (RespBankList) response;
                mList = respBankList.getData();
                mAdapter.setNewData(mList);
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                mLayRefresh.finishRefresh();
                showToast(baseResponse.getMsg());
            }
        });
    }

    /**
     * 解绑银行卡
     *
     * @param id   银行卡ID
     * @param code 验证码
     */
    private void deleteBank(int id, String code) {
        ReqDeleteBank reqDeleteBank = new ReqDeleteBank();
        reqDeleteBank.setId(id);
        reqDeleteBank.setCode(code);
        showLoadingDialog("删除中...");
        VolleyRequestUtils.getInstance().deleteBank(this.toString(), reqDeleteBank, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                dismissLoadingDialog();
                showToast("删除成功");
                mVerifyMobileDialog.dismiss();
                mLayRefresh.autoRefresh();
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                if (!baseResponse.getMsg().contains("验证码")) {
                    mVerifyMobileDialog.dismiss();
                } else {
                    mVerifyMobileDialog.clear();
                }
                dismissLoadingDialog();
                new CustomDialog(BankActivity.this, new CustomDialog.OnClickListener() {
                    @Override
                    public void onLeftClick() {
                    }

                    @Override
                    public void onRightClick() {
                    }
                }).setOneBtn(true).setRight("关闭").setTitle(baseResponse.getMsg()).show();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1234:
                    mLayRefresh.autoRefresh(1000);
                    break;
                default:
                    break;
            }
        }
    }
}
